package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Category;
import com.gaohe.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/page")
    public R page(@RequestParam int page,
                  @RequestParam int pageSize,
                  @RequestParam(required = false) String name){
        Page<Category> categoryPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Category::getName,name);
        qw.orderByDesc(Category::getUpdateTime);
        categoryService.page(categoryPage,qw);
        return R.success(categoryPage);
    }
    //活动修改
    //回显
    @GetMapping("/{id}")
    public R findByid(@PathVariable Long id){
        return R.success(categoryService.getById(id));
    }
    //修改
    @PutMapping
    public R update(@RequestBody Category category)
    {
        boolean b = categoryService.updateById(category);
        return b?R.success("xiugaichengg"):R.error("修改失败");
    }
    //删除
    @DeleteMapping
    public R delete(@RequestParam Long id)
    {
        boolean b = categoryService.removeById(id);
        return b?R.success("删除成功"):R.error("删除失败");
    }
    //新增
    @PostMapping
    public R save(@RequestBody Category category)
    {
        return R.success(categoryService.save(category));
    }
    //商品页面回显
    @GetMapping
    public R findAll(){
        return R.success(categoryService.list());
    }
}
